<?php

/**
 * @see module_base
 */
class module_report_groups extends module_base
{
    function view()
    {
        global $WEB_APP;

        $groups = get_groups('group_name');
        $tests = get_tests('test_name');

        $WEB_APP['show_form'] = TRUE;
        $WEB_APP['show_table'] = FALSE;

        if (!isset($_GET['action']))
        {
            $_GET['action'] = '';
        }

        if ((($_GET['action'] == 'report') || ($_GET['action'] == 'print')) && (count($_POST) == 0))
        {
            if (!isset($_SESSION['report_groups']['last_result']) ||
                !isset($_SESSION['report_groups']['group_array']) ||
                !isset($_SESSION['report_groups']['test_array']) ||
                !isset($_SESSION['report_groups']['testing_period']) ||
                !isset($_SESSION['report_groups']['testing_period_from']) ||
                !isset($_SESSION['report_groups']['testing_period_to']) ||
                !isset($_SESSION['report_groups']['scores']) ||
                !isset($_SESSION['report_groups']['scores_from']) ||
                !isset($_SESSION['report_groups']['scores_to'])
               )
            {
                header('Location: '.$WEB_APP['cfg_url'].'?module=report_groups');
                exit();
            }

            if ($_SESSION['report_groups']['last_result'])
            {
                $WEB_APP['items_count'] = 1;
            }
            else
            {
                $WEB_APP['items_count'] = get_user_results_count_for_groups_report($_SESSION['report_groups']['group_array'],
                    $_SESSION['report_groups']['test_array'], $_SESSION['report_groups']['last_result'],
                    $_SESSION['report_groups']['testing_period'], $_SESSION['report_groups']['testing_period_from'],
                    $_SESSION['report_groups']['testing_period_to'], $_SESSION['report_groups']['scores'], $_SESSION['report_groups']['scores_from'],
                    $_SESSION['report_groups']['scores_to']);
            }

            // Pages count.
            $pages = get_pages_count($WEB_APP['items_count'], ($WEB_APP['count'] == 0) ? $WEB_APP['items_count'] : $WEB_APP['count']);
            if ($WEB_APP['page'] > $pages)
            {
                $WEB_APP['page'] = $pages;
            }

            if (!$_SESSION['report_groups']['generate_csv_file'])
            {
            $user_results = get_groups_report($_SESSION['report_groups']['group_array'],
                $_SESSION['report_groups']['test_array'], $_SESSION['report_groups']['last_result'],
                $_SESSION['report_groups']['testing_period'], $_SESSION['report_groups']['testing_period_from'],
                $_SESSION['report_groups']['testing_period_to'], $_SESSION['report_groups']['scores'], $_SESSION['report_groups']['scores_from'],
                $_SESSION['report_groups']['scores_to'], $_SESSION['report_groups']['testing_time_column'], $WEB_APP['sort_field'],
                        $WEB_APP['sort_order'], $WEB_APP['page'], $WEB_APP['count']);
            }
            if ($_SESSION['report_groups']['generate_csv_file'])
            {
                $user_results = get_groups_report($_SESSION['report_groups']['group_array'],
                $_SESSION['report_groups']['test_array'], $_SESSION['report_groups']['last_result'],
                $_SESSION['report_groups']['testing_period'], $_SESSION['report_groups']['testing_period_from'],
                $_SESSION['report_groups']['testing_period_to'], $_SESSION['report_groups']['scores'], $_SESSION['report_groups']['scores_from'],
                $_SESSION['report_groups']['scores_to'], $_SESSION['report_groups']['testing_time_column']);
                // Generate csv file.
                $report_text = '';

                // Column names.
                if ($_SESSION['report_groups']['number_column'])
                {
                    $report_text .= 'id;';
                }

                if ($_SESSION['report_groups']['group_column'])
                {
                    $report_text .= text('txt_group').';';
                }

                $report_text .= text('txt_name').';';

                if ($_SESSION['report_groups']['login_column'])
                {
                    $report_text .= text('txt_login').';';
                }
				
				if ($_SESSION['report_groups']['user_info_column'])
                {
                    $report_text .= text('txt_info').';';
                }

                if ($_SESSION['report_groups']['test_column'])
                {
                    $report_text .= text('txt_test').';';
                }

                if ($_SESSION['report_groups']['scores_column'])
                {
                    $report_text .= text('txt_score').';';
                }

                if ($_SESSION['report_groups']['results_column'])
                {
                    $report_text .= text('txt_report_results').';';
                }

                if ($_SESSION['report_groups']['correct_column'])
                {
                    $report_text .= text('txt_corrects').';';
                }

                if ($_SESSION['report_groups']['testing_date_column'])
                {
                    $report_text .= text('txt_date').';';
                }

                if ($_SESSION['report_groups']['testing_time_column'])
                {
                    $report_text .= text('txt_time').';';
                }

                if ($_SESSION['report_groups']['completed_column'])
                {
                    $report_text .= text('txt_completed').';';
                }

                if ($_SESSION['report_groups']['percent_column'])
                {
                    $report_text .= text('txt_report_percent').';';
                }

                if ($_SESSION['report_groups']['completed_questions_column'])
                {
                    $report_text .=
                    text('txt_report_completed_questions').';';
                }                

                $report_text .="\r\n";

                foreach($user_results as $user_result)
                {
                    if ($_SESSION['report_groups']['number_column'])
                    {
                        $report_text .= $user_result['id'].';';
                    }

                    if ($_SESSION['report_groups']['group_column'])
                    {
                        $report_text .= $user_result['group_name'].';';
                    }

                    $report_text .= $user_result['user_name'].';';

                    if ($_SESSION['report_groups']['login_column'])
                    {
                        $report_text .= $user_result['user_login'].';';
                    }
					
					if ($_SESSION['report_groups']['user_info_column'])
                    {
                        $report_text .=
                            $user_result['user_info'].';';
                    }

                    if ($_SESSION['report_groups']['test_column'])
                    {
                        $report_text .=
                            $user_result['user_result_test_title'].';';
                    }

                    if ($_SESSION['report_groups']['scores_column'])
                    {
                        $report_text .= $user_result['user_result_score'].';';
                    }

                    if ($_SESSION['report_groups']['results_column'])
                    {
                        $report_text .=
                            $user_result['user_result_results'].';';
                    }

                    if ($_SESSION['report_groups']['correct_column'])
                    {
                        $report_text .=
                        $user_result['user_result_righ_questions'].';';
                    }

                    if ($_SESSION['report_groups']['testing_date_column'])
                    {
                        $report_text .=
                            $user_result['time_begin'].';';
                    }

                    if ($_SESSION['report_groups']['testing_time_column'])
                    {
                        $report_text .=
                            $user_result['test_time'].';';
                    }

                    if ($_SESSION['report_groups']['completed_column'])
                    {
                        $report_text .=
                            $user_result['user_result_completed'].';';
                    }

                    if ($_SESSION['report_groups']['percent_column'])
                    {
                        $report_text .=
                            $user_result['user_result_percent_right'].';';
                    }

                    if ($_SESSION['report_groups']['completed_questions_column'])
                    {
                        $report_text .=
                        $user_result['user_result_completed'].';';
                    }

                    $report_text .="\r\n";
                }

                $csv_file_charset = $WEB_APP['settings']['csv_file_charset'];
                if ($csv_file_charset != 'utf-8')
                {
                    $report_text = iconv('utf-8', $csv_file_charset, $report_text);
                }

                $len = strlen($report_text);
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename=report.csv');
                header('Content-Length: '.$len);

                print($report_text);
                exit();
            }
            $WEB_APP['show_form'] = !$_SESSION['report_groups']['hide_header'];
            $WEB_APP['show_table'] = TRUE;
        }

        if (isset($_POST['scores_from']))
        {
            $correct_post = TRUE;

            if (!isset($_POST['group']))
            {
                $WEB_APP['errorstext'] .= text('txt_select_groups').'<br>';
                $select_groups = array();
                $correct_post = FALSE;
            }
            else
            {
                $_SESSION['report_groups']['group_array'] = $_POST['group'];
                $select_groups = array();
                foreach($_SESSION['report_groups']['group_array'] as $group_id)
                {
                    $group = get_group($group_id);
                    $select_groups[] = $group->name;
                }
            }

            if (!isset($_POST['test']))
            {
                $WEB_APP['errorstext'] .= text('txt_select_tests').'<br>';
                $select_tests = array();
                $correct_post = FALSE;
            }
            else
            {
                $_SESSION['report_groups']['test_array'] = $_POST['test'];
                $select_tests = array();
                foreach($_SESSION['report_groups']['test_array'] as $test_id)
                {
                    $test = get_test($test_id);
                    $select_tests[] = $test->name;
                }
            }

            $_SESSION['report_groups']['last_result'] = isset($_POST['last_result']);
            $_SESSION['report_groups']['hide_header'] = isset($_POST['hide_header']);
            $_SESSION['report_groups']['generate_csv_file'] = isset($_POST['generate_csv_file']);

            // Testing period
            $_SESSION['report_groups']['testing_period'] = isset($_POST['testing_period']);
            $_SESSION['report_groups']['testing_period_from'] = $_POST['testing_period_from'];
            $_SESSION['report_groups']['testing_period_to'] = $_POST['testing_period_to'];

            // Scores
            $_SESSION['report_groups']['scores'] = isset($_POST['scores']);
            $_SESSION['report_groups']['scores_from'] = $_POST['scores_from'];
            $_SESSION['report_groups']['scores_to'] = $_POST['scores_to'];

            // Columns
            $_SESSION['report_groups']['number_column'] = isset($_POST['number_column']);
            $_SESSION['report_groups']['group_column'] = isset($_POST['group_column']);
            $_SESSION['report_groups']['login_column'] = isset($_POST['login_column']);
			$_SESSION['report_groups']['user_info_column'] = isset($_POST['user_info_column']);
            $_SESSION['report_groups']['test_column'] = isset($_POST['test_column']);
            $_SESSION['report_groups']['scores_column'] = isset($_POST['scores_column']);
            $_SESSION['report_groups']['results_column'] = isset($_POST['results_column']);
            $_SESSION['report_groups']['correct_column'] = isset($_POST['correct_column']);
            $_SESSION['report_groups']['testing_date_column'] = isset($_POST['testing_date_column']);
            $_SESSION['report_groups']['testing_time_column'] = isset($_POST['testing_time_column']);
            $_SESSION['report_groups']['completed_column'] = isset($_POST['completed_column']);
            $_SESSION['report_groups']['percent_column'] = isset($_POST['percent_column']);
            $_SESSION['report_groups']['completed_questions_column'] = isset($_POST['completed_questions_column']);            

            if ($correct_post)
            {
                header('Location: '.$WEB_APP['cfg_url'].'?module=report_groups&action=report');
                exit();
            }
        }
        else
        {
            if (!isset($_SESSION['report_groups']['group_array']))
            {
                $_SESSION['report_groups']['group_array'] = array();
            }

            $select_groups = array();
            foreach($_SESSION['report_groups']['group_array'] as $group_id)
            {
                $group = get_group($group_id);
                $select_groups[] = $group->name;
            }

            if (!isset($_SESSION['report_groups']['test_array']))
            {
                $_SESSION['report_groups']['test_array'] = array();
            }
            $select_tests = array();
            foreach($_SESSION['report_groups']['test_array'] as $test_id)
            {
                $test = get_test($test_id);
                $select_tests[] = $test->name;
            }

            if (!isset($_SESSION['report_groups']['last_result']))
            {
                $_SESSION['report_groups']['last_result'] = FALSE;
            }

            if (!isset($_SESSION['report_groups']['hide_header']))
            {
                $_SESSION['report_groups']['hide_header'] = FALSE;
            }

            if (!isset($_SESSION['report_groups']['generate_csv_file']))
            {
                $_SESSION['report_groups']['generate_csv_file'] = FALSE;
            }

            // Testing period
            if (!isset($_SESSION['report_groups']['testing_period']))
            {
                $_SESSION['report_groups']['testing_period'] = FALSE;
            }

            if (!isset($_SESSION['report_groups']['testing_period_from']))
            {
                $_SESSION['report_groups']['testing_period_from'] = date('Y-m-d');
            }

            if (!isset($_SESSION['report_groups']['testing_period_to']))
            {
                $_SESSION['report_groups']['testing_period_to'] = date('Y-m-d');
            }

            //Scores
            if (!isset($_SESSION['report_groups']['scores']))
            {
                $_SESSION['report_groups']['scores'] = FALSE;
            }

            if (!isset($_SESSION['report_groups']['scores_from']))
            {
                $_SESSION['report_groups']['scores_from'] = 0;
            }

            if (!isset($_SESSION['report_groups']['scores_to']))
            {
                $_SESSION['report_groups']['scores_to'] = 1000;
            }

            // Columns
            if (!isset($_SESSION['report_groups']['number_column']))
            {
                $_SESSION['report_groups']['number_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['group_column']))
            {
                $_SESSION['report_groups']['group_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['login_column']))
            {
                $_SESSION['report_groups']['login_column'] = TRUE;
            }
			
			if (!isset($_SESSION['report_groups']['user_info_column']))
            {
                $_SESSION['report_groups']['user_info_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['test_column']))
            {
                $_SESSION['report_groups']['test_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['scores_column']))
            {
                $_SESSION['report_groups']['scores_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['results_column']))
            {
                $_SESSION['report_groups']['results_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['correct_column']))
            {
                $_SESSION['report_groups']['correct_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['testing_date_column']))
            {
                $_SESSION['report_groups']['testing_date_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['testing_time_column']))
            {
                $_SESSION['report_groups']['testing_time_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['completed_column']))
            {
                $_SESSION['report_groups']['completed_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['percent_column']))
            {
                $_SESSION['report_groups']['percent_column'] = TRUE;
            }

            if (!isset($_SESSION['report_groups']['completed_questions_column']))
            {
                $_SESSION['report_groups']['completed_questions_column'] = TRUE;
            }
                        
        }

        // Form fields.
        $fields = array();
        $fields[] = new field(TRUE, text('txt_groups'), 'multiple_select',
                    'group[]', $select_groups, '',
                    $groups, 'id', 'group_name');
        $fields[] = new field(TRUE, text('txt_tests'), 'multiple_select',
                    'test[]', $select_tests, '',
                    $tests, 'id', 'test_name');
        $fields[] = new field(FALSE, text('txt_display_the_last_result_only'),
                    'checkbox', 'last_result', $_SESSION['report_groups']['last_result']);
        $fields[] = new field(FALSE, text('txt_hide_header'), 'checkbox',
                    'hide_header', $_SESSION['report_groups']['hide_header']);
        $fields[] = new field(FALSE, text('txt_generate_csv_file'), 'checkbox',
                    'generate_csv_file', $_SESSION['report_groups']['generate_csv_file']);

        $fields[] = new field(FALSE, text('txt_testing_period'), 'header');
        $fields[] = new field(FALSE, text('txt_testing_period'), 'checkbox',
                    'testing_period', $_SESSION['report_groups']['testing_period']);
        $fields[] = new field(FALSE, text('txt_from'), 'text',
                    'testing_period_from', $_SESSION['report_groups']['testing_period_from']);
        $fields[] = new field(FALSE, text('txt_to'), 'text',
                    'testing_period_to', $_SESSION['report_groups']['testing_period_to']);

        $fields[] = new field(FALSE, text('txt_scores'), 'header');
        $fields[] = new field(FALSE, text('txt_scores'), 'checkbox',
                    'scores', $_SESSION['report_groups']['scores']);
        $fields[] = new field(FALSE,    text('txt_from'), 'text',
                    'scores_from', $_SESSION['report_groups']['scores_from']);
        $fields[] = new field(FALSE, text('txt_to'), 'text',
                    'scores_to', $_SESSION['report_groups']['scores_to']);

        $fields[] = new field(FALSE, text('txt_columns'), 'header');
        $fields[] = new field(FALSE, text('txt_number'), 'checkbox',
                    'number_column', $_SESSION['report_groups']['number_column']);
        $fields[] = new field(FALSE, text('txt_group'), 'checkbox',
                    'group_column', $_SESSION['report_groups']['group_column']);
        $fields[] = new field(FALSE, text('txt_login'), 'checkbox',
                    'login_column', $_SESSION['report_groups']['login_column']);
		$fields[] = new field(FALSE, text('txt_info'), 'checkbox',
                    'user_info_column', $_SESSION['report_groups']['user_info_column']);
        $fields[] = new field(FALSE, text('txt_test'), 'checkbox',
                    'test_column', $_SESSION['report_groups']['test_column']);
        $fields[] = new field(FALSE, text('txt_scores'), 'checkbox',
                    'scores_column', $_SESSION['report_groups']['scores_column']);
        $fields[] = new field(FALSE, text('txt_results'), 'checkbox',
                    'results_column', $_SESSION['report_groups']['results_column']);
        $fields[] = new field(FALSE, text('txt_number_of_correct_answers'),
                    'checkbox', 'correct_column', $_SESSION['report_groups']['correct_column']);
        $fields[] = new field(FALSE, text('txt_testing_date'), 'checkbox',
                    'testing_date_column', $_SESSION['report_groups']['testing_date_column']);
        $fields[] = new field(FALSE, text('txt_testing_time'), 'checkbox',
                    'testing_time_column', $_SESSION['report_groups']['testing_time_column']);
        $fields[] = new field(FALSE, text('txt_completed'), 'checkbox',
                    'completed_column', $_SESSION['report_groups']['completed_column']);
        $fields[] = new field(FALSE, text('txt_percent'), 'checkbox',
                    'percent_column', $_SESSION['report_groups']['percent_column']);
        $fields[] = new field(FALSE, text('txt_completed_questions'), 'checkbox',
                    'completed_questions_column',
                    $_SESSION['report_groups']['completed_questions_column']);        

        $columns = array();


        if ($_SESSION['report_groups']['number_column'])
        {
            $columns[] = new column('id', 'id', 'id');
        }

        if ($_SESSION['report_groups']['group_column'])
        {
            $columns[] = new column('group_name', text('txt_group'), 'group');
        }


        $columns[] = new column('user_name', text('txt_user_name'), 'user');

        if ($_SESSION['report_groups']['login_column'])
        {
            $columns[] = new column('user_login', text('txt_login'), 'login');
        }
		
		if ($_SESSION['report_groups']['user_info_column'])
        {
            $columns[] = new column('user_info',
                text('txt_info'),
                'user_info');
        }

        if ($_SESSION['report_groups']['test_column'])
        {
            $columns[] = new column('user_result_test_title',
                text('txt_test'), 'test_title');
        }

        if ($_SESSION['report_groups']['scores_column'])
        {
            $columns[] = new column('user_result_score', text('txt_score'), 'score');
        }

        if ($_SESSION['report_groups']['results_column'])
        {
            $columns[] = new column('user_result_results',
                text('txt_report_results'), 'results');
        }

        if ($_SESSION['report_groups']['correct_column'])
        {
            $columns[] = new column('user_result_righ_questions',
                text('txt_corrects'), 'righ_questions');
        }

        if ($_SESSION['report_groups']['testing_date_column'])
        {
            $columns[] = new column('time_begin',
                text('txt_date'), 'time_begin');
        }

        if ($_SESSION['report_groups']['testing_time_column'])
        {
            $columns[] = new column('test_time',
                text('txt_time'), 'test_time');
        }

        if ($_SESSION['report_groups']['completed_column'])
        {
            $columns[] = new column('user_result_completed',
                text('txt_completed'),
                'completed');
        }

        if ($_SESSION['report_groups']['percent_column'])
        {
            $columns[] = new column('user_result_percent_right',
                text('txt_report_percent'),
                'percent_right');
        }

        if ($_SESSION['report_groups']['completed_questions_column'])
        {
            $columns[] = new column('user_result_completed_questions',
                text('txt_report_completed_questions'),
                'completed_questions');
        }        

        if ($_GET['action'] == 'report' || $_GET['action'] == 'print')
        {
            $paginator = new paginator($WEB_APP['page'], $pages);
            $paginator->url = $WEB_APP['script_name'];
            $paginator->url_query_array  = $WEB_APP['url_query_array'];
            $WEB_APP['paginator'] = $paginator;
            $WEB_APP['rows'] = $user_results;
            $WEB_APP['rows_count'] = count($user_results);
            $WEB_APP['columns_count'] = count($columns);
            $WEB_APP['columns'] = $columns;
        }

        $WEB_APP['show_table'] = ($_GET['action'] == 'report');

        if (!isset($user_results))
        {
            $user_results = array();
        }
        $WEB_APP['editform'] = TRUE;

        //$WEB_APP['hide_edit'] = 0;
        $WEB_APP['hide_delete'] = 1;

        $WEB_APP['fields'] = $fields;
        $WEB_APP['title_add'] = text('txt_report_settings');
        $WEB_APP['submit_title'] = text('txt_create');
        $WEB_APP['show_empty_value'] = FALSE;
        $WEB_APP['escape'] = FALSE;
        $WEB_APP['title'] = text('txt_group_reports');
        $WEB_APP['submit_title'] = text('txt_create');
        $WEB_APP['row_actions'] = array($WEB_APP['action_details'],
                              $WEB_APP['action_download'],
                              $WEB_APP['action_print_report']
                              );
        if (isset($_GET['action']) && ($_GET['action'] == 'print'))
        {
            $WEB_APP['view']->display('print_array_table.tpl', text('txt_group_reports'), TRUE);
        }
        else
        {
            $WEB_APP['print_url'] = $WEB_APP['cfg_url'].'?module=report_groups&action=print&sort='.$WEB_APP['sort_field'].'&order='.$WEB_APP['sort_order'].'&page='.$WEB_APP['page'];
            if ($_GET['action'] != '')
            {
                $WEB_APP['action'] = $_GET['action'];
            }
            $WEB_APP['view']->display('table_array_rows.tpl',
                text('txt_group_reports'));
        }
    }

    function details()
    {
        global $WEB_APP;
        header('Location: '.$WEB_APP['cfg_url'].'?module=report_user_results&action=details&id='.$WEB_APP['id']);
        exit();
    }

    function download()
    {
        global $WEB_APP;
        header('Location: '.$WEB_APP['cfg_url'].'?module=report_user_results&action=download&id='.$WEB_APP['id']);
        exit();
    }

    function print_report()
    {
        global $WEB_APP;
        header('Location: '.$WEB_APP['cfg_url'].'?module=report_user_results&action=print_report&id='.$WEB_APP['id']);
        exit();
    }
}

